#include "user_include.h"
#include <stdio.h>
#include "nvs_flash.h"
#include <unistd.h>
#include <pthread.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_pthread.h"


void app_main(void)
{
    pthread_t user_thread;
    esp_err_t ret;
    int res;

    // Initialize NVS.
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK( ret );

    res = pthread_create(&user_thread, NULL, (void *)&user_ble_task, NULL);
    assert(res == 0);
    printf("Created ble thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_wifi_task, NULL);
    assert(res == 0);
    printf("Created wifi thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_command_task, NULL);
    assert(res == 0);
    printf("Created command thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_mqtt_task, NULL);
    assert(res == 0);
    printf("Created mqtt thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_led_task, NULL);
    assert(res == 0);
    printf("Created led thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_key_task, NULL);
    assert(res == 0);
    printf("Created key thread 0x%"PRIx32" with new default config\n", user_thread);
    
    res = pthread_create(&user_thread, NULL, (void *)&user_aht20_task, NULL);
    assert(res == 0);
    printf("Created aht20 thread 0x%"PRIx32" with new default config\n", user_thread);

    res = pthread_create(&user_thread, NULL, (void *)&user_timer_task, NULL);
    assert(res == 0);
    printf("Created aht20 thread 0x%"PRIx32" with new default config\n", user_thread);
    
}

